
package com.yimixian.app.cart;

import com.yimixian.app.SystemFramework;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.goods.GoodsViewModel;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.CartDeliverGoodsGroup;
import com.yimixian.app.model.CartUpdate;
import com.yimixian.app.model.CartUpdateGoodsGroup;
import com.yimixian.app.model.Category;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.SortGoodsItem;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.Strings;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.json.JSONException;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CartManager {
    private static CartManager sInstance;
    public static CartManager getInstance() {
        if (sInstance == null) {
            sInstance = new CartManager();
            try {
                Object obj = SharedPreferencesHelper.getObject(SharePreferenceKeys.GOOD_J);
                offLineCartMap = (null!=obj&&obj instanceof HashMap)?(HashMap)obj:new HashMap();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sInstance;
    }
    public int mTotalQuantity;
    public float mTotalPrice;
    public boolean mHasFreebie = false;
    public GoodsItemPresenter.OnGoodsQuantityChangedListener mOnGoodsQuantityChangedListener;
    public List<CartUpdateGoodsGroup> mGoodsGroup;//按配送方式分组商品列表
    public CartUpdate mCartUpdate;
    public Map<Integer,GoodsItem> mCartGoodsItems;//购物车中所有商品的ID与商品的映射
    public final Map<Integer, GoodsItem> mIdToGoodsItem;
    private WeakReference<CartView> mCartViewReference;
    private WeakReference<CartNewView> mCartNewViewReference;
    private TextView mTotalQuantityText;// 购物车的物品数量，目前在用它显示金额
    public String mCurrentState = "";// 不同Fragment的标示
    public long lastRevision = 0;

    //离线购物车数据
    public double offlineTotalPrice = 0;//购物车商品总价

    public CartManager() {
        mIdToGoodsItem = new HashMap<>();
        mGoodsGroup = new ArrayList<>();
        mCartGoodsItems = new HashMap<>();
        clearCart();
    }
    //退出登录重新计算购物车商品总价  退出时不保存，清空本地数据
    /*public void onQuitLogin() {
        offlineTotalPrice = mTotalPrice;
        updateCartView();
        setCartQuantityText();
        EventBus.getDefault().post(new String(Constants.EVENT_BUS_HOME_FRAGMENT_REFRESH));
        EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_UPDATE_DESCRIPTION));
    }*/
    public void clearCart() {
//        mIdToQuantity.clear();
        mCartGoodsItems.clear();
        System.out.println("哈利路亚清1****");
//        mCurrentItems.clear();
        mGoodsGroup.clear();
        mTotalQuantity = 0;
        mTotalPrice = 0f;
        mHasFreebie = false;
        updateCartView();
        setCartQuantityText();
        EventBus.getDefault().post(new String(Constants.EVENT_BUS_HOME_FRAGMENT_REFRESH));
        EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_UPDATE_DESCRIPTION));
    }

    public void setCartNewView(CartNewView cartNewView) {
        mCartNewViewReference = new WeakReference<>(cartNewView);
        updateCartView();
    }

    private void updateCartView() {
        // if (mCartViewReference != null && mCartViewReference.get() != null) {
        // mCartViewReference.get().setBadgeNumber(mTotalQuantity);
        // mCartViewReference.get().setTotalPrice(mTotalPrice);
        // }
        if (mCartNewViewReference != null && mCartNewViewReference.get() != null) {
            mCartNewViewReference.get().setTotalPrice(mCartUpdate);
//            mCartNewViewReference.get().setIsFreeSend(mTotalPrice);
        }
        sendQuantityChangeBroadCast();
    }

    public void sendQuantityChangeBroadCast() {
        Intent intent = new Intent(Constants.QUANTITY_CHANGE_BROADCAST);
        intent.putExtra(Constants.QUANTITY_CHANGE, mTotalQuantity);
        LocalBroadcastManager.getInstance(SystemFramework.getInstance().getGlobalContext())
                .sendBroadcast(intent);
        EventBus.getDefault().post(new String(Constants.CART_TOTAL_PRICE_CHANGE_BROADCAST));
    }

    public void registerGoodsItemPresenter(GoodsItemPresenter presenter) {
        // presenter.setButtonListener(this);
        // presenter.setQuantity(getQuantity(presenter.getGoodsItemId()));
    }

//    public int getQuantity(int goodsId) {
//        return mIdToQuantity.containsKey(goodsId) ? mIdToQuantity.get(goodsId) : 0;
//    }

    public GoodsItem getItem(int goodsId) {
        return mIdToGoodsItem.get(goodsId);
    }
    public String getFormatTotalPrice(){
        String str = "";
        if(isUserLogin()){
            if(mCartUpdate!=null){
                str = mCartUpdate.total.totalPrice;
            }
        }else{
            str = offlineTotalPrice>0?"总价:￥ " + Strings.getDecimalWith2Number(offlineTotalPrice):"";
//            if(mTotalQuantityText != null)
//                mTotalQuantityText.setVisibility(offlineTotalPrice == 0 ? View.GONE : View.VISIBLE);
        }

        return str;
    }

    /**设置购物车旁边价格文本*/
    public void setCartPriceData(TextView view){
        if(!CartManager.getInstance().isUserLogin()) {
            double price = offlineTotalPrice<=0?(double)SharedPreferencesHelper.getFloat(SharePreferenceKeys.CART_TOTAL_PRICE,0):offlineTotalPrice;
            if(price>0){
                view.setVisibility(View.VISIBLE);
                view.setText("总价:￥ " + Strings.getDecimalWith2Number(price));
                if(offlineTotalPrice<=0)this.offlineTotalPrice = price;
            }
            Object obj = SharedPreferencesHelper.getObject(SharePreferenceKeys.GOOD_J);
            if(null!=obj&&obj instanceof HashMap){
                offLineCartMap = (HashMap)obj;
            }
            if(mTotalQuantityText != null)
                mTotalQuantityText.setVisibility(offlineTotalPrice == 0 ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * 专为首页数据构造
     * @param sortGoodsItem
     */
    public void setSortSections(List<SortGoodsItem> sortGoodsItem) {
        if(sortGoodsItem == null || sortGoodsItem.size() == 0) {
            return ;
        }
        mHasFreebie = false;
        for (SortGoodsItem sortItem : sortGoodsItem) {
            GoodsItem leftItem = sortItem.leftGoodsItem;
            if (leftItem != null){
                if(mIdToGoodsItem.containsKey(leftItem.id) && mIdToGoodsItem.get(leftItem.id) != null) {
                    String count = mIdToGoodsItem.get(leftItem.id).count;
                    mIdToGoodsItem.put(leftItem.id, leftItem);
                    mIdToGoodsItem.get(leftItem.id).count = count;
                } else {
                    mIdToGoodsItem.put(leftItem.id, leftItem);
                }
            }
            GoodsItem rightItem = sortItem.RightGoodsItem;
            if (rightItem != null){
                if(mIdToGoodsItem.containsKey(rightItem.id) && mIdToGoodsItem.get(rightItem.id) != null) {
                    String count = mIdToGoodsItem.get(rightItem.id).count;
                    mIdToGoodsItem.put(rightItem.id, rightItem);
                    mIdToGoodsItem.get(rightItem.id).count = count;
                } else {
                    mIdToGoodsItem.put(rightItem.id, rightItem);
                }
            }
        }
        setCartQuantityText();
        updateCartView();
    }


    public void setSections(List<GoodsItem> goods) {
        if(goods == null || goods.size() == 0) {
            return ;
        }
        mHasFreebie = false;
        for (GoodsItem item : goods) {
            if (mIdToGoodsItem.containsKey(item.id) && mIdToGoodsItem.get(item.id) != null) {
                String count = mIdToGoodsItem.get(item.id).count;
                mIdToGoodsItem.put(item.id, item);
                mIdToGoodsItem.get(item.id).count = count;
            } else {
                mIdToGoodsItem.put(item.id, item);
            }
        }
//        setCartQuantityText();
        updateCartView();
    }

    public List<GoodsViewModel> getViewModels(Category category) {
//        for (Section section : mSections) {
//            if (section.id == sectionId) {
                List<GoodsViewModel> viewModels = new ArrayList<>();
                //添加分类（原首页）的顶部Banner
//                if (section.banners != null && section.banners.size() > 0) {
//                    viewModels.add(new GoodsViewModel(section.banners, section.bannersImgRatio,
//                            null));
//                }
                for (GoodsItem item : category.goods) {
                    viewModels.add(new GoodsViewModel(null, 0, item));
                }
                return viewModels;
//            }
//        }
    }

    /**
     * 返回TypeGift的广播
     *
     * @param flag
     */
//    public void sendTypeGiftBroadCast(boolean flag, int goodsId) {
//        PlusMinusIconMessage message = new PlusMinusIconMessage(mPlusMinusIcon, goodsId, flag,
//                Constants.TYPE_GIFT_FLAG_BROADCAST);
//        EventBus.getDefault().post(message);
//        mPlusMinusIcon = null;
//    }

    /**
     * 设置去结算的状态问题
     */
    private void dealCartSelectOkBtn() {
        if (mCartNewViewReference != null && mCartNewViewReference.get() != null) {
            mCartNewViewReference.get().setSelectOkClickable(mTotalQuantity == 0 ? false : true);
        }
    }

    /**
     * 设置购物车模块下方Tab的数量显示
     */
    public void setCartQuantityText() {
        boolean mHasGoods;
        if (mTotalQuantityText != null) {
            if(isUserLogin()){
                if(null!=mCartUpdate&&null!=mCartUpdate.total)
                mTotalQuantityText.setText(mCartUpdate.total.totalPrice);
                mHasGoods = mTotalQuantity > 0;
            }else{
                mTotalQuantityText.setText("总价:￥ " + Strings.getDecimalWith2Number(offlineTotalPrice));
                mHasGoods = offlineTotalPrice > 0;
            }

            mTotalQuantityText.setVisibility((mHasGoods || offlineTotalPrice != 0) ? View.VISIBLE : View.GONE);
        }else{
        }
    }
    /**
     * 设置购物车模块下方Tab的金额显示
     */
    private void setCartQuantityText(double currentTotalPrice) {
        if (mTotalQuantityText != null) {
            offlineTotalPrice = isAdd? offlineTotalPrice + currentTotalPrice: offlineTotalPrice - currentTotalPrice;
            BigDecimal bd = new BigDecimal(offlineTotalPrice);
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            offlineTotalPrice = bd.doubleValue();
            mTotalQuantityText.setText("总价:￥ " + Strings.getDecimalWith2Number(offlineTotalPrice));
            SharedPreferencesHelper.commitFloat(SharePreferenceKeys.CART_TOTAL_PRICE,(float) (offlineTotalPrice < 0 ? 0 : offlineTotalPrice));
            boolean mHasGoods = offlineTotalPrice > 0;
            mTotalQuantityText.setVisibility(mHasGoods ? View.VISIBLE : View.GONE);
        }
    }

    public BaseAdapter getGoodsAdapter(Context context) {
        return new GoodsListAdapter(context);
    }

    private class GoodsListAdapter extends BaseAdapter {

        private Context mContext;
        private List<Integer> mGoodsIds;

        public GoodsListAdapter(Context context) {
            mContext = context;
            mGoodsIds = new ArrayList<>();
            int freebieId = -1;
            for (Integer goodsId : mCartGoodsItems.keySet()) {
                if (mCartGoodsItems.get(goodsId) != null && Integer.parseInt(mCartGoodsItems.get(goodsId).count) > 0) {
                    if (mIdToGoodsItem.get(goodsId).typeGift != null) {
                        freebieId = goodsId;
                    } else {
                        mGoodsIds.add(goodsId);
                    }
                }
            }
            if (freebieId != -1) {
                mGoodsIds.add(freebieId);
            }
        }

        @Override
        public int getCount() {
            return mGoodsIds.size();
        }

        @Override
        public GoodsItem getItem(int position) {
            return mIdToGoodsItem.get(mGoodsIds.get(position));
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getItemViewType(position) == 0 ? getNormalView(position, convertView)
                    : getFreebieView(position, convertView);
        }

        private View getNormalView(int position, View convertView) {
            if (convertView == null) {
                convertView = new CartGoodsItemView(mContext);
            }
            CartGoodsItemView itemView = (CartGoodsItemView) convertView;
            itemView.bind(getItem(position));
            itemView.setShowDividerBelow(position < getCount() - (mHasFreebie ? 2 : 1));
            registerGoodsItemPresenter(itemView);

            return itemView;
        }

        private View getFreebieView(int position, View convertView) {
            if (convertView == null) {
                AppConfig appConfig = DataManager.getInstance().get(CacheKeys.APP_CONFIG);
                convertView = new CartFreebieGoodsItemView(mContext, appConfig == null ? ""
                        : appConfig.cartGiftTitle);
            }
            CartFreebieGoodsItemView itemView = (CartFreebieGoodsItemView) convertView;
            itemView.bind(getItem(position));
            itemView.setShowDividerBelow(false);
            registerGoodsItemPresenter(itemView);

            return itemView;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return getItem(position).typeGift == null ? 0 : 1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }
    }

    // 设置下方的购物车数量的TextView
    public void setCartQuantityTextView(TextView mTotalQuantityText) {
        this.mTotalQuantityText = mTotalQuantityText;
    }

    // 标示Button被点击的状态
    public void setBroadCastState(String state) {
        this.mCurrentState = state;
    }

    public void updateDataSource(int totalQuantity,
                                 double totalPrice,PlusMinusIcon plusMinusIcon){
        mTotalQuantity = totalQuantity;
        mTotalPrice = (float)totalPrice;
        setCartQuantityText(totalPrice);
        sendQuantityChangeBroadCast();
        dealCartSelectOkBtn();
        // 刷新plusMinusIcon中数量的显示问题
        if (plusMinusIcon != null && plusMinusIcon.mGoodsItem != null
            && plusMinusIcon.mButtonState != null) {
            plusMinusIcon
                .updateQutity(
                    plusMinusIcon.mButtonState
                        .equals(Constants.PLUS_BUTTON_CLICKED_BROADCAST) ? true : false,
                    plusMinusIcon.mGoodsItem.id);
        }
    }

    public void updateDataSource(GoodsItem item,int totalQuantity,
            double totalPrice, long revision) {
        if (revision < lastRevision) {
            return;
        }
        lastRevision = revision;
        // 更新原始数据
        if (mIdToGoodsItem != null && item != null) {
            mIdToGoodsItem.put(item.id,item);
        }
        mTotalQuantity = totalQuantity;
        mTotalPrice = (float) totalPrice;
        setCartQuantityText();
        updateCartView();
        dealCartSelectOkBtn();

    }

    public void updateDataSource(int totalQuantity,
            double totalPrice, long revision) {
        if (revision < lastRevision) {
            return;
        }
        lastRevision = revision;
        mTotalQuantity = totalQuantity;
        mTotalPrice = (float) totalPrice;
        setCartQuantityText();
        updateCartView();
        dealCartSelectOkBtn();

    }

    public void updateCartGoods(CartUpdate cartUpdate, PlusMinusIcon plusMinusIcon) {
        if(cartUpdate == null) {
            return ;
        }

        mCartUpdate = cartUpdate;

        List<CartDeliverGoodsGroup> deliverGoods = cartUpdate.delieverGoodsGroup;
        if (mGoodsGroup == null) mGoodsGroup = new ArrayList<>();
        else
            mGoodsGroup.clear();
        if (deliverGoods == null){
            return;
        }
        for (int i = 0; i < deliverGoods.size(); i++){
            CartDeliverGoodsGroup cartDeliverGoodsGroup = deliverGoods.get(i);
            List<CartUpdateGoodsGroup> cartUpdateGoodsGroups = cartDeliverGoodsGroup.goodsGroup;
            if (cartUpdateGoodsGroups != null && cartUpdateGoodsGroups.size() > 0){
                mGoodsGroup.addAll(cartUpdateGoodsGroups);
            }
        }
        if (mGoodsGroup == null || plusMinusIcon == null) {
            return;
        }

        // 处理 1->0 时的显示问题
        boolean needSetGoodsQuantityTo0 = true;
        List<Integer> goodsIds = new ArrayList<>();
        for (int i = 0; i < mGoodsGroup.size(); i++) {
            for (int j = 0; j < mGoodsGroup.get(i).goods.size(); j++) {
                goodsIds.add(mGoodsGroup.get(i).goods.get(j).id);
            }
        }
        if (plusMinusIcon.mGoodsItem != null && goodsIds.contains(plusMinusIcon.mGoodsItem.id)) {
            needSetGoodsQuantityTo0 = false;
        }
        //TODO 购物车数量
        if (needSetGoodsQuantityTo0) {
            if (mCartGoodsItems != null && plusMinusIcon.mGoodsItem != null && mCartGoodsItems.get(plusMinusIcon.mGoodsItem.id) != null) {
                updateDataSource(mCartGoodsItems.get(plusMinusIcon.mGoodsItem.id),
                    cartUpdate.total.count, cartUpdate.total.price, cartUpdate.revision);
                mCartGoodsItems.remove(plusMinusIcon.mGoodsItem.id);
            }
            if (mCartGoodsItems != null && plusMinusIcon.mGoodsItem != null) {
                if(mCartGoodsItems.get(plusMinusIcon.mGoodsItem.id) == null){
                    updateDataSource(mCartGoodsItems.get(plusMinusIcon.mGoodsItem.id),
                            0, cartUpdate.total.price, cartUpdate.revision);
                    mCartGoodsItems.remove(plusMinusIcon.mGoodsItem.id);
                }
            }
        }

        // 更新数据源
        for (int j = 0; j < mGoodsGroup.size(); j++) {
            for (int i = 0; i < mGoodsGroup.get(j).goods.size(); i++) {
                GoodsItem item = mGoodsGroup.get(j).goods.get(i);
                if (item != null) {
                    updateDataSource(item,
                            cartUpdate.total.count, cartUpdate.total.price, cartUpdate.revision);
                }
            }
        }

        // 刷新plusMinusIcon中数量的显示问题
        if (plusMinusIcon != null && plusMinusIcon.mGoodsItem != null
                && plusMinusIcon.mButtonState != null) {
            plusMinusIcon
                .updateQutity(
                    plusMinusIcon.mButtonState
                        .equals(Constants.PLUS_BUTTON_CLICKED_BROADCAST) ? true : false,
                    plusMinusIcon.mGoodsItem.id);
        }
    }
    public void updateCartGoods(CartUpdate cartUpdate) {
        if(cartUpdate == null) {
            return ;
        }
        mCartUpdate = cartUpdate;
        List<CartDeliverGoodsGroup> deliverGoods = cartUpdate.delieverGoodsGroup;
        if (mGoodsGroup == null) mGoodsGroup = new ArrayList<>();
        else
            mGoodsGroup.clear();
        if (deliverGoods == null){
            return;
        }
        for (int i = 0; i < deliverGoods.size(); i++){
            CartDeliverGoodsGroup cartDeliverGoodsGroup = deliverGoods.get(i);
            List<CartUpdateGoodsGroup> cartUpdateGoodsGroups = cartDeliverGoodsGroup.goodsGroup;
            if (cartUpdateGoodsGroups != null && cartUpdateGoodsGroups.size() > 0){
                mGoodsGroup.addAll(cartUpdateGoodsGroups);
            }
        }

        if (mGoodsGroup == null) {
            return;
        }

        // 更新数据源
        Set<Integer> backIds = new HashSet<>();
        for (int j = 0; j < mGoodsGroup.size(); j++) {
            for (int i = 0; i < mGoodsGroup.get(j).goods.size(); i++) {
                GoodsItem item = mGoodsGroup.get(j).goods.get(i);
                if (item != null) {
                    backIds.add(item.id);
                    updateDataSource(item,
                            cartUpdate.total.count, cartUpdate.total.price, cartUpdate.revision);
                }
            }
        }

        // 处理商品过期时候的问题
        Set<Integer> currentItems = new HashSet<>();
        currentItems.addAll(mCartGoodsItems.keySet());
        currentItems.removeAll(backIds);
        if (currentItems.size() > 0 && mCartGoodsItems != null) {
            for (Iterator iter = currentItems.iterator(); iter.hasNext();) {
                int goodsId = (int)iter.next();
                updateDataSource(mCartGoodsItems.get(goodsId),
                    cartUpdate.total.count, cartUpdate.total.price, cartUpdate.revision);
                mCartGoodsItems.remove(goodsId);
            }
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_GET_CART));
        }

//         更新没有返回商品时的数据源
        if (cartUpdate.total != null)
        {
            updateDataSource(cartUpdate.total.count, cartUpdate.total.price, cartUpdate.revision);
        }
    }

    public void updateAddCartGoodsItem(List<GoodsItem> goodsItems) {
        if(goodsItems == null) {
            return ;
        }
        for(int i=0;i<goodsItems.size();i++) {
            if(goodsItems.get(i) != null) {
                mIdToGoodsItem.put(goodsItems.get(i).id, goodsItems.get(i));
            }
        }
    }

    public boolean isUserLogin(){
        return DataManager.getInstance().get(CacheKeys.TOKEN) != null;
    }

    private int currentStoreID;
    public void saveStoreID(int storeID){
        currentStoreID = storeID;
        SharedPreferencesHelper.commitInt(SharePreferenceKeys.STORE_ID, storeID);
    }
    public int getOfflineGoodCount(int goodId){
        if(offLineCartMap!=null&&offLineCartMap.size()>0&&offLineCartMap.containsKey(goodId)){
            return Integer.parseInt(offLineCartMap.get(goodId).count);
        }
        return -1;
    }
    public static HashMap<Integer, GoodsItem> offLineCartMap;
    private boolean isAdd;
    GoodsItem item;
    /**保存离线状态购物车数据*/
    public void saveUnLoginCartData(int goodID,boolean isAdd,PlusMinusIcon plusMinusIcon) throws JSONException {
        this.isAdd = isAdd;
        int count = 0;
        if(mCartGoodsItems!=null)item = mCartGoodsItems.get(goodID);
        if(null==item){
            item = plusMinusIcon.mGoodsItem;
            count = 0;
        }else if(item.count!=null&&Integer.parseInt(item.count)>0){
            count = Integer.parseInt(item.count);
        }
        count = isAdd?(count+1):(count==0?0:count-1);
        item.count = count+"";
        if(null!=offLineCartMap){
            offLineCartMap.put(goodID, item);
        }
        saveUnLoginCartDataToDisc();
        updateGoodItem(goodID, count, plusMinusIcon);
    }
    /**程序退出时保存离线购物车数据*/
    public void saveUnLoginCartDataToDisc(){
        if(null!=offLineCartMap&&offLineCartMap.size()>0){
            SharedPreferencesHelper.saveObject(SharePreferenceKeys.GOOD_J, offLineCartMap);
        }
    }

    /**更新单条数据*/
    private void updateGoodItem(int goodID,int goodCount,PlusMinusIcon plusMinusIcon){
//        GoodsItem matchItem = null;
        if(null!=mCartUpdate){
            List<CartDeliverGoodsGroup> deliverGoods = mCartUpdate.delieverGoodsGroup;
            for (CartDeliverGoodsGroup cartDeliverGoodsGroup: deliverGoods){
                for (CartUpdateGoodsGroup cartUpdateGoodsGroup :cartDeliverGoodsGroup.goodsGroup){
                    for(GoodsItem item:cartUpdateGoodsGroup.goods){
                        if(null!=item&&item.id == goodID){
                            item.count = goodCount+"";
                            mCartGoodsItems.put(goodID,item);
                            mIdToGoodsItem.put(goodID,item);
                        }
                    }
                }
            }
        }
        mCartGoodsItems.put(goodID,plusMinusIcon.mGoodsItem);
        mIdToGoodsItem.put(goodID,plusMinusIcon.mGoodsItem);
        if (mCartGoodsItems != null && plusMinusIcon.mGoodsItem != null && plusMinusIcon.mGoodsItem != null) {
            updateDataSource(goodCount, Double.parseDouble(plusMinusIcon.mGoodsItem.unitPrice), plusMinusIcon);
            if (goodCount==0) {
                mCartGoodsItems.remove(plusMinusIcon.mGoodsItem.id);
            }
        }
    }

    public void clearOfflineData(){
        SharedPreferencesHelper.clearObject(SharePreferenceKeys.GOOD_J);
        SharedPreferencesHelper.clearInt(SharePreferenceKeys.STORE_ID);
        SharedPreferencesHelper.clearFloat(SharePreferenceKeys.CART_TOTAL_PRICE);
        offlineTotalPrice = 0;
        if(null!=offLineCartMap)offLineCartMap.clear();
//                CartManager.getInstance().saveUnLoginCartDataToDisc();
//                CartManager.getInstance().mCartGoodsItems.clear();
//        mIdToGoodsItem.clear();
//        mGoodsGroup.clear();
    }

}
